import pandas
import pathlib
import os
import tkinter.filedialog
import shutil
import uuid

# 初始的运行路径
InitRunPath = os.getcwd()

# 获取当前脚本所在的路径
CurrentPath = pathlib.Path(__file__).parent.absolute().resolve().__str__()

# 获取当前用户的桌面路径
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")

# 切换运行路径到脚本所在路径
os.chdir(CurrentPath)

# 获取需要复制文件内容的所在文件夹路径
source_folder = tkinter.filedialog.askdirectory(
    title="选择源文件夹",
    initialdir=str(CurrentPath)
)

if not source_folder:
    print("未选择源文件夹，程序退出")
    exit()

# 扫描所有需要拷贝复制的文件和一级子文件夹
items_to_copy = []
for item in os.listdir(source_folder):
    item_path = os.path.join(source_folder, item)
    items_to_copy.append(item_path)

# 获取增强助手的评价文件夹
target_folder = tkinter.filedialog.askdirectory(
    title="选择 [增强助手 的 评价文件夹]",
    initialdir=str(CurrentPath)
)

if not target_folder:
    print("未选择目标文件夹，程序退出")
    exit()

# 获取评价文件夹下的所有一级子文件夹
evaluation_folders = []
for item in os.listdir(target_folder):
    item_path = os.path.join(target_folder, item)
    if os.path.isdir(item_path):
        evaluation_folders.append(item_path)

# 对每个评价文件夹进行复制操作
total_items = len(items_to_copy)
for i, eval_folder in enumerate(evaluation_folders):
    print(f"\n处理文件夹 {i+1}/{len(evaluation_folders)}: {eval_folder}")
    
    for j, item in enumerate(items_to_copy):
        item_name = os.path.basename(item)
        target_path = os.path.join(eval_folder, item_name)
        
        try:
            # 确保目标目录存在
            os.makedirs(os.path.dirname(target_path), exist_ok=True)
            
            if os.path.isfile(item):
                shutil.copy2(item, target_path)
                print(f"  文件 {j+1}/{total_items}: {item_name} [成功]")
            elif os.path.isdir(item):
                if not os.path.exists(target_path):
                    shutil.copytree(item, target_path)
                    print(f"  目录 {j+1}/{total_items}: {item_name} [成功]")
                else:
                    print(f"  目录 {j+1}/{total_items}: {item_name} [已存在，跳过]")
            else:
                print(f"  未知类型 {j+1}/{total_items}: {item_name} [跳过]")
        except Exception as e:
            print(f"  错误 {j+1}/{total_items}: {item_name} → {str(e)}")

print("\n操作完成！")